package com.xxxx.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.server.mapper.SalaryMapper;
import com.xxxx.server.pojo.Salary;
import com.xxxx.server.service.ISalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

import static java.time.LocalDateTime.now;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2020-08-17
 */
@Service
public class SalaryServiceImpl extends ServiceImpl<SalaryMapper, Salary> implements ISalaryService {
    @Resource
    private SalaryMapper salaryMapper;

    /**
     * 添加工资账套
     * @param salary
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public int addSalary(Salary salary) {
        //通过mybatis-plus 单表直接添加，删除，更新
        //设置默认值
        salary.setCreateDate(now());
        //添加操作
        return salaryMapper.insert(salary);
        /*
        参数校验
         */
        
        /*
        启用时间
        createDate
        设置默认值
         */
//        salary.setCreateDate(LocalDateTime.now());
        
        
        /*
        执行添加操作
         */
//        return salaryMapper.addSalaryUser(salary);
        
        
    }

    /**
     * 更新工资账套
     * @param salary
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateSalary(Salary salary) {
        //根据条件进行更新
        UpdateWrapper<Salary> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",salary.getId());
        //使用mybatis plus 的crud接口update进行选择更新
        return salaryMapper.update(salary, updateWrapper);
    }

    /**
     * 删除工资账套
     * 此处无逻辑删除字段，采用彻底删除
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public int deleteSalary(Integer id) {
        //根据工资账套id使用mybatis plus直接删除操作
//        return salaryMapper.deleteById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        return salaryMapper.deleteByMap(map);
        
    }

    /**
     * 员工账套查询
     */
    
}
